import 'package:flutter/material.dart';
class CustomComponent extends StatelessWidget {
final bool isHidden;
CustomComponent({required this.isHidden});
@override
Widget build(BuildContext context) {
return Visibility(
visible: !isHidden,
child: Container(
width: 200,
height: 200,
color: Colors.blue,
child: Center(
child: Text(
'Custom Component',
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
),
),
),
);
}
}
class CustomController {
late VoidCallback _listener;
bool _isHidden = false;
bool get isHidden => _isHidden;
set isHidden(bool value) {
_isHidden = value;
_listener();
}
void addListener(VoidCallback listener) {
_listener = listener;
}
void removeListener() {
_listener = () {};
}
}
class CustomScreen extends StatefulWidget {
@override
_CustomScreenState createState() => _CustomScreenState();
}
class _CustomScreenState extends State<CustomScreen> {
late CustomController _controller;
@override
void initState() {
super.initState();
_controller = CustomController();
_controller.addListener(_handleControllerChange);
}
@override
void dispose() {
_controller.removeListener();
super.dispose();
}
void _handleControllerChange() {
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Custom Component Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CustomComponent(
isHidden: _controller.isHidden,
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
_controller.isHidden = !_controller.isHidden;
},
child: Text(
_controller.isHidden ? 'Show' : 'Hide',
),
),
],
),
),
);
}
}
void main() {
runApp(MaterialApp(
home: CustomScreen(),
));
}